package com.cqc.dialogdemo;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.style.BulletSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	protected static final String TAG = "MainActivity";
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	private Button btn6;
	private Context context;
	private String[] items;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;

		findViews();
		initViews();
	}

	private void initViews() {
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
	}

	private void findViews() {
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn3 = (Button) findViewById(R.id.btn3);
		btn4 = (Button) findViewById(R.id.btn4);
		btn5 = (Button) findViewById(R.id.btn5);
		btn6 = (Button) findViewById(R.id.btn6);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn1:
			initAlertDialog();
			break;
		case R.id.btn2:
			initSingleChoiceDialog();
			break;
		case R.id.btn3:
			initMultiChoiceDialog();
			break;
		case R.id.btn4:
			initProgressDialog();
			break;
		case R.id.btn5:
			initCustomDialog();
			break;
		case R.id.btn6:
			initCustomDialog2();
			break;
		default:
			break;
		}
	}

	private void initCustomDialog2() {
		final Dialog dialog = new Dialog(context, R.style.MyStyle);// 去掉dialog的title，dialog区域外是灰色
		// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//
		// 去掉dialog的title,放在设置布局前
		View view = View.inflate(context, R.layout.custom_dialog2, null);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(true);// 点击dialog意外的区域，dialog是否消失

		Window window = dialog.getWindow();// 创建当前窗体
		window.setGravity(Gravity.BOTTOM);// 当前床底位于底部

		// 添加动画
		window.setWindowAnimations(R.style.DialogAnimationStyle);

		// 设置 宽 + 高
		LayoutParams params = window.getAttributes();// 获取当前窗体的参数
		view.measure(0, 0);
		params.width = getResources().getDisplayMetrics().widthPixels;
		params.height = view.getMeasuredHeight();// 不设置高的话，dialog会充满屏幕，下方是白色，即使在布局中设置了高的具体值

		window.setAttributes(params);

		Button btn_camera = (Button) dialog.findViewById(R.id.btn_camera);
		Button btn_gallery = (Button) dialog.findViewById(R.id.btn_gallery);
		Button btn6_cancle = (Button) dialog.findViewById(R.id.btn6_cancle);

		btn_camera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(context, "btn_camera");
				dialog.dismiss();
			}
		});

		btn_gallery.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(context, "btn_gallery");
				dialog.dismiss();
			}
		});

		btn6_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ToastUtil.showShortToast(context, "btn6_cancle");
				dialog.dismiss();
			}
		});

		dialog.show();// 显示dialog
	}

	private void initCustomDialog() {
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉dialog的title,放在设置布局前
		dialog.setContentView(R.layout.custom_dialog);

		dialog.getWindow().setGravity(Gravity.BOTTOM);

		Button btn_ok = (Button) dialog.findViewById(R.id.btn_ok);
		Button btn_cancle = (Button) dialog.findViewById(R.id.btn_cancle);

		btn_ok.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		btn_cancle.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void initProgressDialog() {
		final ProgressDialog dialog = new ProgressDialog(context);
		dialog.setTitle("Title");// 设置标题
		dialog.setMessage("message----message");// 设置dialog内容
		dialog.setIcon(R.drawable.ic_launcher);// 设置图标，与为Title左侧
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 圆形进度条
//		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 水平线进度条
//		dialog.setMax(100);// 最大值
//		new Thread() {
//			public void run() {
//				for (int i = 0; i <= 100; i++) {
//					SystemClock.sleep(50);
//					dialog.setProgress(i);// 每隔50毫秒设置当前进度
//				}
//				dialog.dismiss();// for执行完成后dialog默认不消失
//			};
//		}.start();
		dialog.show();
	}

	private void initMultiChoiceDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Title");
		// builder.setMessage("message--message");//设置message就没有了单选列表
		builder.setIcon(R.drawable.ic_launcher);
		items = new String[] { "item1", "item2", "item3", "item4", "item5" };
		final boolean[] checkedItems = new boolean[] { false, false, false,
				false, false };
		builder.setMultiChoiceItems(items, checkedItems,
				new DialogInterface.OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						ToastUtil.showShortToast(context, items[which]);
					}
				});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				ToastUtil.showShortToast(context, "取消");
				// dialog.dismiss();//可以不调用dismiss(),dialog会自动消失
			}
		});
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < checkedItems.length; i++) {
					if (checkedItems[i]) {
						sb.append(items[i]);
						
					}
				}
				ToastUtil.showShortToast(context, "确定" + sb.toString());
				// dialog.dismiss();可以不调用dismiss(),dialog会自动消失
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	private void initSingleChoiceDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("标题");
		// builder.setMessage("message--message");//设置message就没有了单选列表
		builder.setIcon(R.drawable.ic_launcher);
		final String[] items = new String[] { "item1", "item2", "item3",
				"item4", "item5" };
		final boolean[] checkedItem = new boolean[] { false, false, false,
				false, false };
		builder.setSingleChoiceItems(items, -1,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						checkedItem[which] = true;
						ToastUtil.showShortToast(context, items[which]);
						dialog.dismiss();// 点击单选按钮，dialog不会自动消失
					}
				});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	// 普通对话框
	private void initAlertDialog() {
		Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("更新提示");
		builder.setMessage("这是知乎最新版本，请更新！");
		builder.setIcon(R.drawable.ic_launcher);
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				ToastUtil.showShortToast(context, "确定" + which);
				// dialog.dismiss();//可以不调用dismiss(),dialog会自动消失
			}
		});
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				ToastUtil.showShortToast(context, "取消" + which);
				// dialog.dismiss();可以不调用dismiss(),dialog会自动消失
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

}
